package com.zzyl.serve.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService iNursingProjectPlanService;
    @Autowired
    private INursingPlanService nursingPlanService;
    @Autowired
    private RedisTemplate redisTemplate;
    private static final String CACHE_KEY_PREFIX = "nursingPlan:all";

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id)
    {
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        NursingPlan nursingPlan = getById(id);
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        List< NursingProjectPlan> nursingProjectPlans=iNursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        List<NursingProjectPlanVo> nursingProjectPlanVos=nursingProjectPlans.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            nursingProjectPlanVo.setProjectId(nursingProjectPlan.getProjectId()+"");
            nursingProjectPlanVo.setExecuteTime(nursingProjectPlan.getExecuteTime());
            nursingProjectPlanVo.setExecuteCycle(nursingProjectPlan.getExecuteCycle()+"");
            nursingProjectPlanVo.setExecuteFrequency(nursingProjectPlan.getExecuteFrequency());
            nursingProjectPlanVo.setId(nursingProjectPlan.getId());
            nursingProjectPlanVo.setPlanId(Long.valueOf(nursingProjectPlan.getPlanId()));
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());
        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
       NursingPlan nursingPlan =new NursingPlan();
       BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
      this.save(nursingPlan);
      List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
      if (!CollectionUtils.isEmpty(projectPlans)){
          projectPlans.forEach(projectPlan -> {
              projectPlan.setPlanId(Integer.valueOf(nursingPlan.getId()+""));
              projectPlan.setCreateTime(new Date());
              projectPlan.setUpdateTime(new Date());
          });
      }
     boolean flag =iNursingProjectPlanService.saveBatch(projectPlans);
      return flag ? 1 : 0;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
      List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
      if (!CollectionUtils.isEmpty(projectPlans)){
          iNursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlanDto.getId()));
          projectPlans.forEach(ProjectPlan -> {
              ProjectPlan.setPlanId(Integer.valueOf( nursingPlanDto.getId()+""));


          });
          iNursingProjectPlanService.saveBatch(projectPlans);
      }
      NursingPlan nursingPlan =new NursingPlan();
      BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        boolean flag = updateById(nursingPlan);
        return flag ? 1 : 0;
    }


    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNursingPlanById(Integer id) {
       iNursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        boolean b = removeByIds(Arrays.asList(id));
        return b ? 1 : 0;
    }

    @Override
    public List<NursingPlan> listAll() {
         // 从缓存中获取
        List<NursingPlan> nursingPlans = (List<NursingPlan>) redisTemplate.opsForValue().get( CACHE_KEY_PREFIX);
        // 缓存中有数据，直接返回
        if ( nursingPlans != null  && nursingPlans.size() > 0){
            return nursingPlans;
        }
        // 缓存中没有数据，从数据库中查询
        LambdaQueryWrapper<NursingPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NursingPlan::getStatus, 1);
        List<NursingPlan> list = list(queryWrapper);
        // 将数据写入缓存
        redisTemplate.opsForValue().set("nursingPlans", list);
        return list;
    }
}
